package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.constant.FileConstant;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.template.MinioTemplate;
import com.base.vistter.iframe.utils.IoUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@Slf4j
public class DownloadController {

    @Resource(name = "minioTemplate")
    private MinioTemplate minioTemplate;

    @RequestMapping("/download")
    public void download(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws IframeException {
        String fileName = filePath.substring(filePath.lastIndexOf(FileConstant.SPRIT) + 1);
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
        IoUtils.fileToStream(filePath , response);
    }

}
